﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Mammoth.Entities;
using Mammoth.Repositories;
using Mammoth.Services;

namespace Mammoth.ViewModels
{
    public class AddValasViewModel : Libs.WPFViewModelBase
    {
        public string Name { get; set; }
        public decimal Sell { get; set; }
        public decimal Buy1 { get; set; }
        public decimal Buy2 { get; set; }
        public decimal Buy3 { get; set; }
        public decimal Type1 { get; set; }
        public decimal Type2 { get; set; }
        public decimal Type3 { get; set; }

        IRepositoryFactory repo;

        public AddValasViewModel(IInputElement dispatcher, IRepositoryFactory repo) : base(dispatcher)
        {
            this.repo = repo;
        }

        public void Save()
        {
            if (!Validate()) return;

            try
            {
                using (var r = repo.GetRepository<ValasEntity>())
                {
                    r.Save(new ValasEntity()
                    {
                        Name = Name, Sell = Sell, Buy1 = Buy1,
                        Buy2 = Buy2, Buy3 = Buy3, Type1 = Type1,
                        Type2 = Type2, Type3 = Type3
                    });
                }
                Dispatcher.RaiseNavigationEvent();
            }
            catch (Exception)
            {
                Dispatcher.RaiseErrorEvent("Gagal menyimpan ke database");
            }
        }
    }
}
